namespace Spotted.Storage.Tables.Pages
{
    using System;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Navigation;
    using Spotted.Storage.Tables.ViewModels;

    public partial class SampleDataDetailsPage
    {
        public SampleDataDetailsPage()
        {
            this.InitializeComponent();

            var viewModel = new SampleDataDetailsPageViewModel();
            viewModel.SaveChangesSuccess += (s, e) => this.NavigationService.GoBack();

            this.ViewModel = viewModel;
        }

        public SampleDataDetailsPageViewModel ViewModel
        {
            get { return this.DataContext as SampleDataDetailsPageViewModel; }
            set { this.DataContext = value; }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var editSampleDataAsString = this.NavigationContext.QueryString["editSampleData"];
            bool editSampleData;

            if (string.IsNullOrWhiteSpace(editSampleDataAsString) || !bool.TryParse(editSampleDataAsString, out editSampleData))
                editSampleData = false;

            if (this.ViewModel.SampleData == null)
            {
                var sampleData = ApplicationStateHelpers.Get<SampleData>("CurrentSampleDataRow");
                if (editSampleData && (sampleData == null))
                    this.NavigationService.GoBack();

                this.ViewModel.SetSampleDataModel(sampleData);
                ApplicationStateHelpers.Remove("CurrentSampleDataRow");
            }
        }

        private void OnTextBoxKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (sender == this.PartitionKeyTextBox)
                    this.RowKeyTextBox.Focus();
                else if (sender == this.RowKeyTextBox)
                    this.NameTextBox.Focus();
                else if (sender == this.NameTextBox)
                    this.DescriptionTextBox.Focus();
                else if (sender == this.DescriptionTextBox)
                    this.NumberTextBox.Focus();
                else if (sender == this.NumberTextBox)
                    this.DateDatePicker.Focus();
                else
                {
                    this.PartitionKeyTextBox.Focus();
                }
            }
        }

        private void OnDeleteSampleData(object sender, EventArgs e)
        {
            this.ViewModel.DeleteSampleData();
        }

        private void OnSaveSampleData(object sender, EventArgs e)
        {
            var focusTextbox = FocusManager.GetFocusedElement() as TextBox;
            if (focusTextbox != null)
            {
                var binding = focusTextbox.GetBindingExpression(TextBox.TextProperty);
                if (binding != null)
                    binding.UpdateSource();
            }

            this.ViewModel.SaveSampleData();
        }
    }
}